package org.easymis.air.southwest;





import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;




@Controller
public class IndexAction {
	@Bean
	@LoadBalanced
	public RestTemplate getRestTemplate() {
		return new RestTemplate();
	}
	@RequestMapping(value="/",method=RequestMethod.GET)
	public String login(HttpSession session, ModelMap model, HttpServletRequest request) throws Exception {		
		String memberName = request.getParameter("memberName");// 账户名
		String password = request.getParameter("password");// 用户密码
		// FIXME 由于系统本身原因初始化进入的时候就调用该接口，而传值为空直接略过了前端效验，进入了该方法。
		if (memberName == null || password == null) {
			request.setAttribute("base", "http://demo.easymis.cn");
			return "login";
		} else {
			
		}
		return "index";
	}
	@RequestMapping(value="/router",method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE )
	public String router() {
		RestTemplate restTpl = getRestTemplate();
		System.out.println("router begin!!!");
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());    	
		String json=restTpl.getForObject("http://127.0.0.1:8080/person/1000",String.class);
		System.out.println(json);
		return json;
	}
}
